<?php

namespace app\index\controller;

use app\common\controller\Base;
use app\common\model\Codes;
use app\common\model\InoculationModel;
use app\common\model\OrderModel;
use app\common\model\UserModel;
use app\index\model\User;
use Cassandra\Date;
use think\Db;
use think\Log;
use think\Request;
use think\Validate;

class Order extends Base
{

    /**
     * @Notes:创建序列号
     * @Interface createOrder
     * @param Request $request
     * @author: kevin.yong
     * @Time: 2020/4/2 0002 13:39
     */
    public function createOrder()
    {
        for ($i = 0; $i < 10000; $i++) {
            $str = OrderModel::random_str(6);
            echo $str . "<br>";
            Db::name("order")->insert(["sn" => $str, "create_time" => time()]);

        }
        $this->successful('创建序列号成功');
    }




    /**
     * @Notes:绑定序列号
     * @Interface makeOrder
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: kevin.yong
     * @Time: 2020/4/2 0002 13:39
     */
    public function bindOrder(Request $request)
    {

        try {
            Db::startTrans();
            $rule = [
                'user_id' => ['require'],
                'sn' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'sn.require' => '序列号不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }

            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            $model = OrderModel::hasSn($sn);
            if (!$model) {
                $this->failed('序列号不存在,请重新输入');
            }

            if (empty($userInfo->sn)) {
                if ($model->status == 2) {
                    $this->failed('序列号已使用,请重新输入');
                }
                $userInfo->sn = $sn;
                $userInfo->update_time = time();
                $userInfo->force()->save();
                Db::name("order")->where("sn", $sn)->update(["status" => 2, "update_time" => time()]);


            } else {
                if ($model->status == 1) {
                    $this->failed('您已经绑定序列号,请重新输入');
                }

                if ($model->status == 2 && $userInfo->sn != $model->sn) {
                    $this->failed('序列号已使用,请重新输入');
                }

                $userInfo->update_time = time();
                $userInfo->force()->save();

            }


            Db::commit();
            $this->successful('开启成功');
        } catch (\Exception $e) {
            Db::rollback();
            Log::error("===开启失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('开启失败');
        }
    }


    /**
     * @Notes:绑定鲁班序列号
     * @Interface bindOrderLuban
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: kevin.yong
     * @Time: 2020/4/2 0002 13:39
     */
    public function bindOrderLuban(Request $request)
    {

        try {
            Db::startTrans();
            $rule = [
                'user_id' => ['require'],
                'sn_luban' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'sn_luban.require' => '鲁班序列号不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }

            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            $model = OrderModel::hasSn($sn_luban);
            if (!$model) {
                $this->failed('序列号不存在,请重新输入');
            }

            if (empty($userInfo->sn_luban)) {
                if ($model->status == 2) {
                    $this->failed('序列号已使用,请重新输入');
                }
                $userInfo->sn_luban = $sn_luban;
                $userInfo->update_time = time();
                $userInfo->force()->save();
                Db::name("order")->where("sn", $sn_luban)->update(["status" => 2, "update_time" => time()]);


            } else {
                if ($model->status == 1) {
                    $this->failed('您已经绑定序列号,请重新输入');
                }

                if ($model->status == 2 && $userInfo->sn_luban != $model->sn) {
                    $this->failed('序列号已使用,请重新输入');
                }

                $userInfo->update_time = time();
                $userInfo->force()->save();

            }


            Db::commit();
            $this->successful('开启鲁班档案成功');
        } catch (\Exception $e) {
            Db::rollback();
            Log::error("===开启鲁班档案失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('开启鲁班档案失败');
        }
    }


    /**
     * @Notes:绑定花花序列号
     * @Interface bindOrderHuahua
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: kevin.yong
     * @Time: 2020/4/2 0002 13:39
     */
    public function bindOrderHuahua(Request $request)
    {

        try {
            Db::startTrans();
            $rule = [
                'user_id' => ['require'],
                'sn_huahua' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'sn_huahua.require' => '花花序列号不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }

            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            $model = OrderModel::hasSn($sn_huahua);
            if (!$model) {
                $this->failed('序列号不存在,请重新输入');
            }

            if (empty($userInfo->sn_huahua)) {
                if ($model->status == 2) {
                    $this->failed('序列号已使用,请重新输入');
                }
                $userInfo->sn_huahua = $sn_huahua;
                $userInfo->update_time = time();
                $userInfo->force()->save();
                Db::name("order")->where("sn", $sn_huahua)->update(["status" => 2, "update_time" => time()]);


            } else {
                if ($model->status == 1) {
                    $this->failed('您已经绑定序列号,请重新输入');
                }

                if ($model->status == 2 && $userInfo->sn_huahua != $model->sn) {
                    $this->failed('序列号已使用,请重新输入');
                }

                $userInfo->update_time = time();
                $userInfo->force()->save();

            }


            Db::commit();
            $this->successful('开启花花档案成功');
        } catch (\Exception $e) {
            Db::rollback();
            Log::error("===开启花花档案失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('开启花花档案失败');
        }
    }


    /**
     * @Notes:绑定鲁达序列号
     * @Interface bindOrderLuda
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: kevin.yong
     * @Time: 2020/4/2 0002 13:39
     */
    public function bindOrderLuda(Request $request)
    {

        try {
            Db::startTrans();
            $rule = [
                'user_id' => ['require'],
                'sn_luda' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'sn_luda.require' => '鲁达序列号不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }

            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            $model = OrderModel::hasSn($sn_luda);
            if (!$model) {
                $this->failed('序列号不存在,请重新输入');
            }

            if (empty($userInfo->sn_luda)) {
                if ($model->status == 2) {
                    $this->failed('序列号已使用,请重新输入');
                }
                $userInfo->sn_luda = $sn_luda;
                $userInfo->update_time = time();
                $userInfo->force()->save();
                Db::name("order")->where("sn", $sn_luda)->update(["status" => 2, "update_time" => time()]);


            } else {
                if ($model->status == 1) {
                    $this->failed('您已经绑定序列号,请重新输入');
                }

                if ($model->status == 2 && $userInfo->sn_luban != $model->sn) {
                    $this->failed('序列号已使用,请重新输入');
                }

                $userInfo->update_time = time();
                $userInfo->force()->save();

            }


            Db::commit();
            $this->successful('开启鲁达档案成功');
        } catch (\Exception $e) {
            Db::rollback();
            Log::error("===开启鲁达档案失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('开启鲁达档案失败');
        }
    }


    /**
     * @Notes:解锁鲁班关卡
     * @Interface makeOrderLuban
     * @param Request $request
     * @author: kevin.yong
     * @Time: 2020/4/10 0010 9:40
     */
    public function makeOrderLuban(Request $request)
    {

        try {
            $rule = [
                'user_id' => ['require'],
                'grade_luban' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'grade_luban.require' => '鲁班关卡等级不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }
            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            if (empty($userInfo->sn_luban)) {
                $this->failed('请先绑定鲁班序列号');
            }
            $userInfo->grade_luban = $grade_luban;
            $userInfo->update_time = time();
            $userInfo->force()->save();

            $this->successful('解锁鲁班成功');
        } catch (\Exception $e) {
            Log::error("===解锁鲁班失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('解锁鲁班失败');
        }
    }


    /**
     * @Notes:解锁花花关卡
     * @Interface makeOrderHuahua
     * @param Request $request
     * @author: kevin.yong
     * @Time: 2020/4/10 0010 9:39
     */
    public function makeOrderHuahua(Request $request)
    {

        try {
            $rule = [
                'user_id' => ['require'],
                'grade_huahua' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'grade_huahua.require' => '花花关卡等级不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }
            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            if (empty($userInfo->sn_huahua)) {
                $this->failed('请先绑定花花序列号');
            }
            $userInfo->grade_huahua = $grade_huahua;
            $userInfo->update_time = time();
            $userInfo->force()->save();

            $this->successful('解锁花花成功');
        } catch (\Exception $e) {
            Log::error("===解锁花花失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('解锁花花失败');
        }
    }


    /**
     * @Notes:解锁鲁达关卡
     * @Interface makeOrderLuda
     * @param Request $request
     * @author: kevin.yong
     * @Time: 2020/4/10 0010 9:39
     */
    public function makeOrderLuda(Request $request)
    {

        try {
            $rule = [
                'user_id' => ['require'],
                'grade_luda' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'grade_luda.require' => '鲁班关卡等级不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }
            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            if (empty($userInfo->sn_luda)) {
                $this->failed('请先绑定鲁达序列号');
            }
            $userInfo->grade_luda = $grade_luda;
            $userInfo->update_time = time();
            $userInfo->force()->save();

            $this->successful('解锁鲁达成功');
        } catch (\Exception $e) {
            Log::error("===解锁鲁达失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('解锁鲁达失败');
        }
    }


    /**
     * @Notes:解锁主线关卡
     * @Interface makeOrderMain
     * @param Request $request
     * @author: kevin.yong
     * @Time: 2020/4/10 0010 9:39
     */
    public function makeOrderMain(Request $request)
    {

        try {
            $rule = [
                'user_id' => ['require'],
                'grade_main' => ['require'],
            ];
            $message = [
                'user_id.require' => '请先登录',
                'grade_main.require' => '主线关卡等级不能为空',
            ];

            $validate = Validate::make($rule, $message);
            $result = $validate->check($request->post());
            if (!$result) {
                $this->failed($validate->getError());
            }
            extract($request->post());

            $userInfo = UserModel::hasUserId($user_id);
            if (!$userInfo) {
                $this->failed('请先登录');
            }

            if (empty($userInfo->sn_luban)) {
                $this->failed('请先绑定鲁班序列号');
            }
            if (empty($userInfo->sn_huahua)) {
                $this->failed('请先绑定花花序列号');
            }
            if (empty($userInfo->sn_luda)) {
                $this->failed('请先绑定鲁达序列号');
            }
            $userInfo->grade_main = $grade_main;
            $userInfo->update_time = time();
            $userInfo->force()->save();

            $this->successful('解锁主线成功');
        } catch (\Exception $e) {
            Log::error("===解锁主线失败===", $e->getMessage());
            // $this->failed($e->getMessage());
            $this->failed('解锁主线失败');
        }
    }
}
